package com.dushiguang.findhoney.Utils.DataStorage.SharedPreferences;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.dushiguang.findhoney.Key.Config;


/**
 * 使用SharedPreference来储存数据
 * Created by ex-dushiguang201 on 2015-11-23.
 */
public class SharedPreferenceService {

    private SharedPreferences sp;
    private Editor mEditor;

    public SharedPreferenceService(Context context) {
        sp = context.getSharedPreferences(Config.SHAREDPREFERENCE, Activity.MODE_PRIVATE);
        mEditor = sp.edit();
    }

    public String getStringValue(String key) {
        if(null != sp) {
            return sp.getString(key, "value");
        }
        return null;
    }

    public int getIntValue(String key) {
        if(null != sp) {
            return sp.getInt(key, -1);
        }
        return -1;
    }

    public boolean getBooleanValue(String key) {
        if(null != sp) {
            return sp.getBoolean(key, false);
        }
        return false;
    }

    public void putStringValue(String key, String value) {
        if (null != mEditor) {
            mEditor.putString(key, value);
            mEditor.commit();
        }
    }

    public void putIntValue(String key, int value) {
        if (null != mEditor) {
            mEditor.putInt(key, value);
            mEditor.commit();
        }
    }

    public void putBooleanValue(String key, boolean value) {
        if (null != mEditor) {
            mEditor.putBoolean(key, value);
            mEditor.commit();
        }
    }

    /**
     * 清空所有数据
     */
    public void cleanAllData() {
        mEditor.clear();
        mEditor.commit();
    }

    /**
     * 删除数据
     */
    public void deleteData(String key) {
        mEditor.remove(key);
        mEditor.commit();
    }

}
